﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class TouristrouteController : Controller
    {
        //注入
        private ITouristrouteService _touristrouteService;
        public TouristrouteController(ITouristrouteService touristrouteService)
        {
            _touristrouteService = touristrouteService;
        }

        #region 查询旅游路线列表
        /// <summary>
        /// 查询旅游路线列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetTouristrouteList(int page, int limit,string data)
        {
            ResponseResult responseResult = _touristrouteService.GetTouristrouteList(page, limit, data);
            return Json(responseResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加或编辑旅游路线
        /// <summary>
        /// 添加或编辑旅游路线
        /// </summary>
        /// <param name="touristroute"></param>
        /// <returns></returns>
        public JsonResult AddTouristRoute(Touristroute touristroute)
        {
            ResponseResult responseResult = new ResponseResult();
            //根据ID是否为空判断是添加还是编辑操作
            if (touristroute.Id > 0)
            {
                //实例化对象接收根据ID查询返回的一个对象
                Touristroute tour = _touristrouteService.Query(a=>a.Id==touristroute.Id);
                #region 逐个赋值
                //逐个赋值
                tour.ProductNumber = touristroute.ProductNumber;
                tour.OutPlace = touristroute.OutPlace;
                tour.OverPlace = touristroute.OverPlace;
                tour.TName = touristroute.TName;
                tour.Price = touristroute.Price;
                tour.OutDate = touristroute.OutDate;
                tour.Duration = touristroute.Duration;
                tour.RouteIntroduce = touristroute.RouteIntroduce;
                tour.TDate = touristroute.TDate;
                tour.IsThemeTour = touristroute.IsThemeTour;
                tour.HotelId = touristroute.HotelId;
                tour.ScenicSpotId = touristroute.ScenicSpotId; 
                #endregion
                //编辑旅游路线
                responseResult.Success = _touristrouteService.Update(tour);
                responseResult.msg = responseResult.Success ? "编辑成功" : "编辑失败";
            }
            else
            {
                //添加旅游路线
                responseResult.Success = _touristrouteService.Add(touristroute);
                responseResult.msg = responseResult.Success ? "添加成功" : "添加失败";
            }
            return Json(responseResult);
        }
        #endregion

        #region 修改主题状态
        /// <summary>
        /// 修改主题状态
        /// </summary>
        /// <param name="touristroute"></param>
        /// <returns></returns>
        public JsonResult UpdateTouristRoute(int Id,int IsThemeTour)
        {
            ResponseResult responseResult = new ResponseResult();
            Touristroute touristroute= _touristrouteService.Query(a=>a.Id==Id);
            touristroute.IsThemeTour = IsThemeTour;
            responseResult.Success=_touristrouteService.Update(touristroute);
            responseResult.msg = responseResult.Success ? "成功" : "失败";
            return Json(responseResult);
        }
        #endregion

        #region 根据ID删除旅游信息
        /// <summary>
        /// 根据ID删除旅游信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult DelTouristRoute(int Id)
        {
            ResponseResult responseResult = new ResponseResult();
            //调用删除的方法
            responseResult.Success = _touristrouteService.DeleteById(Id);
            responseResult.msg = responseResult.Success ? "删除成功" : "删除失败";
            return Json(responseResult);
        }
        #endregion

        #region 批量删除旅游信息
        /// <summary>
        /// 批量删除旅游信息
        /// </summary>
        /// <param name="ListId"></param>
        /// <returns></returns>
        public JsonResult BatchDeleteById(string ListId)
        {
            ResponseResult responseResult = new ResponseResult();
            //将string字符串转化为int数组
            int[] channelCIdArr = Array.ConvertAll(ListId.Split(','), s => int.Parse(s));
            //调用批量删除的方法
            responseResult.Success = _touristrouteService.BatchDeleteById(channelCIdArr);
            responseResult.msg = responseResult.Success ? "批量删除成功" : "批量删除失败";
            return Json(responseResult);
        }
        #endregion
    }
}